package com.ruoyi.miniapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.miniapp.MiniAppUser;
import com.ruoyi.common.security.annotation.Anonymous;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.map.SoMap;
import com.ruoyi.miniapp.domain.TMiniHistory;
import com.ruoyi.miniapp.service.IMiniAppUserService;
import com.ruoyi.miniapp.service.ITMiniHistoryService;
import com.ruoyi.miniapp.service.ITMiniSportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 小程序信息Controller
 * 
 * @author 张力
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/basics/miniapps")
public class MiniAppStatController extends BaseController
{
    @Autowired
    private IMiniAppUserService miniAppUserService;

    @Autowired
    private ITMiniHistoryService miniHistoryService;

    @Autowired
    private ITMiniSportRecordService miniSportRecordService;

    /**
     * 查询小程序信息列表
     */
    @Anonymous
    @GetMapping("/statsUser")
    public AjaxResult statsUser()
    {
        SoMap soMap = getRequestSoMap();
        // 查询用户数
        Long userCount = miniAppUserService.count();
        //昨日新增用户数
        Long userCountYesterday = miniAppUserService.count(new LambdaQueryWrapper<MiniAppUser>().eq(MiniAppUser::getCreateTime, DateUtils.getDateBeforeDays(-1)));
        // 查询昨日体重打卡用户数
        Long weightCountYesterday = miniHistoryService.count(new LambdaQueryWrapper<TMiniHistory>().eq(TMiniHistory::getCreateTime, DateUtils.getDateBeforeDays(-1)));
        // 查询昨日运动打卡用户数 由于运动记录表中一个用户有多条运动记录 所以需要去重
        long sportCountYesterday =  miniSportRecordService.getSportRecordUserCount(soMap);

        Map respMap = new HashMap();
        respMap.put("userCount", userCount);
        respMap.put("userCountYesterday", userCountYesterday);
        respMap.put("weightCountYesterday", weightCountYesterday);
        respMap.put("sportCountYesterday", sportCountYesterday);
        return AjaxResult.success(respMap);
    }
}
